package com.imedway.ihe.dd.service.impl;

import com.imedway.ihe.dd.dto.RetrieveDTO;
import com.imedway.ihe.dd.helper.Iti43RetrieveHelper;
import com.imedway.ihe.dd.service.Iti43ClientService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.RetrieveDocumentSetRequestType;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.RetrieveDocumentSetResponseType;
import org.openehealth.ipf.commons.ihe.xds.core.requests.RetrieveDocumentSet;
import org.openehealth.ipf.commons.ihe.xds.core.responses.RetrievedDocument;
import org.openehealth.ipf.commons.ihe.xds.core.responses.RetrievedDocumentSet;
import org.openehealth.ipf.commons.ihe.xds.iti43.Iti43PortType;
import org.openehealth.ipf.platform.camel.ihe.xds.core.converters.EbXML30Converters;

import org.springframework.stereotype.Service;

import javax.xml.namespace.QName;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

/**
 * @author bovane bovane.ch@gmial.com
 * @create 2024/7/26
 */
@Service
@Slf4j
public class Iti43ClientServiceImpl implements Iti43ClientService {

    public Iti43PortType getIti43PortType(RetrieveDTO retrieveDTO) throws MalformedURLException {
        String iti43ServiceWsdl = retrieveDTO.getIti43ServiceWsdl();
        String iti43ServiceNamespaceUri = retrieveDTO.getIti43ServiceNamespaceUri();
        String iti43ServiceName = retrieveDTO.getIti43ServiceName();


        // 创建WSDL地址,不是服务地址
        URL wsdlUrl = new URL(iti43ServiceWsdl);
        // 1.namespaceURI - 命名空间地址 (wsdl文档中的targetNamespace) 如 targetNamespace="urn:ihe:iti:xds-b:2007"
        // 2.localPart - 服务视图名  (wsdl文档中服务名称，例如<service name="DocumentRepository_Service">)
        QName serviceName = new QName(iti43ServiceNamespaceUri, iti43ServiceName);
        javax.xml.ws.Service service = javax.xml.ws.Service.create(wsdlUrl, serviceName);
        // 获取服务实现类
        // 参数解释:serviceEndpointInterface - 服务端口
        // (wsdl文档中服务端口的name属性，例如<wsdl:port name="MobileCodeWSSoap" binding="tns:MobileCodeWSSoap">)
        // 当然这里 ITI43 事务里面的 port ———————— 就是 Iti43PortType
        Iti43PortType myWebService = service.getPort(Iti43PortType.class);
        return myWebService;
    }

    @Override
    public String retrieveDocument(RetrieveDTO retrieveDTO) throws IOException {
        Iti43PortType iti43WebService = getIti43PortType(retrieveDTO);
        RetrieveDocumentSet request = Iti43RetrieveHelper.createRetrieveDocumentSet(retrieveDTO);

        // 转换类型,符合Webservice要求,并发送请求
        RetrieveDocumentSetRequestType requestType = EbXML30Converters.convert(request);
        RetrieveDocumentSetResponseType responseType = iti43WebService.documentRepositoryRetrieveDocumentSet(requestType);

        // 转为 RetrievedDocumentSet 方便处理文档
        RetrievedDocumentSet retrievedDocumentSet = EbXML30Converters.convert(responseType);
        List<RetrievedDocument> retrievedDocuments = retrievedDocumentSet.getDocuments();
        String documentContent = null;
        for (RetrievedDocument retrievedDocument : retrievedDocuments) {
            log.warn("文件的MimeType是啥: " + retrievedDocument.getMimeType());
            documentContent = IOUtils.toString(retrievedDocument.getDataHandler().getInputStream());
            log.info("文件的内容是啥: " + documentContent);

        }
        return documentContent;
    }
}
